﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Eflong.MonitoringLib.Common;


namespace KWBridge2009
{
    public partial class AuthorizationForm : Form
    {
        private KWBridgeForm mainform;
        public AuthorizationForm(KWBridgeForm form)
        {
            mainform = form;
            InitializeComponent();

            GetAuthoringInfo();

        }

        private void GetAuthoringInfo()
        {
            tbAuthorization.Text = mainform.strMD5String;
            tbCoalMineName.Text = mainform.CoalMineName;
            tbCoalMineNo.Text = mainform.CoalMineId.ToString();
            tbMacAddress.Text = mainform.strMacAddress;
        }

        private void SaveAuthoringInfo()
        {
            mainform.strMacAddress = tbMacAddress.Text;
            mainform.strMD5String = tbAuthorization.Text;
            mainform.CoalMineName = tbCoalMineName.Text;
            uint n = 0;
            uint.TryParse(tbCoalMineNo.Text, out n);
            mainform.CoalMineId = n;

            mainform.SetAuthoringRegistryKey();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            string strSource = tbCoalMineName.Text + "eflongHb@" + tbMacAddress.Text + tbCoalMineNo.Text;
            string strMd5 = KWConvert.md5(strSource).Substring(13,8);
            if (strMd5 == tbAuthorization.Text)
            {
                SaveAuthoringInfo();
                MessageBox.Show(this, "授权通过,\n将重新启动接口程序", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                mainform.Restart("");
            }
            else
            {
                DialogResult result = MessageBox.Show(this, "授权未通过，您需要重新输入(Yes)或退出程序(No)", "提示", MessageBoxButtons.YesNo);
                if (result== DialogResult.Yes)
                {
                    tbAuthorization.Text = "";
                } 
                else
                {
                    Application.Exit();
                }
            }
        }

        private void AuthorizationForm_Load(object sender, EventArgs e)
        {

        }
    }
}
